package com.platform.modules.chat.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.DesensitizedUtil;
import com.platform.common.version.ApiVersion;
import com.platform.common.version.VersionEnum;
import com.platform.common.web.controller.BaseController;
import com.platform.common.web.domain.AjaxResult;
import com.platform.common.web.page.PageDomain;
import com.platform.common.web.page.TableDataInfo;
import com.platform.common.web.page.TableSupport;
import com.platform.modules.chat.domain.ChatUser;
import com.platform.modules.chat.service.ChatApplyService;
import com.platform.modules.chat.service.ChatFriendService;
import com.platform.modules.chat.service.ChatUserService;
import com.platform.modules.chat.vo.ApplyVo01;
import com.platform.modules.chat.vo.FriendVo02;
import com.platform.modules.chat.vo.MyVo09;
import com.platform.modules.wallet.domain.Wallet;
import com.platform.modules.wallet.domain.WalletConfig;
import com.platform.modules.wallet.service.WalletConfigService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 申请
 */
@RestController
@Slf4j
@RequestMapping("/apply")
public class ApplyController extends BaseController {

    @Resource
    private ChatFriendService chatFriendService;

    @Resource
    private ChatApplyService chatApplyService;
    @Resource
    private ChatUserService userService;
    @Resource
    private WalletConfigService configService;

    /**
     * 申请添加
     */
    @ApiVersion(VersionEnum.V1_0_0)
    @PostMapping("/add")
    public AjaxResult add(@Validated @RequestBody FriendVo02 friendVo) {
        chatFriendService.applyFriend(friendVo);
        return AjaxResult.success();
    }

    /**
     * 添加客服
     */
    @ApiVersion(VersionEnum.V1_0_0)
    @PostMapping("/addService")
    public AjaxResult addService(@Validated @RequestBody FriendVo02 friendVo) {
        try {
            chatFriendService.applyService(friendVo);
        }catch (Exception e){
            e.printStackTrace();
        }
        return AjaxResult.success();
    }

    /**
     * 获取客服
     */
    @ApiVersion(VersionEnum.V1_0_0)
    @PostMapping("/getService")
    public AjaxResult getService() {
        String[] serviceKefu = configService.getServiceKefu();
        List<MyVo09> list=new ArrayList<>();
        for (String s : serviceKefu) {
            ChatUser cu = userService.getById(Long.parseLong(s));
            MyVo09 myVo09 = BeanUtil.toBean(cu, MyVo09.class)
                    .setPhone(DesensitizedUtil.mobilePhone(cu.getPhone()));
            list.add(myVo09);
        }
        return AjaxResult.success(list);
    }


    /**
     * 申请记录
     */
    @ApiVersion(VersionEnum.V1_0_0)
    @GetMapping("/list")
    public AjaxResult list() {
        return AjaxResult.success(chatApplyService.list());
    }

    /**
     * 申请详情
     */
    @ApiVersion(VersionEnum.V1_0_0)
    @GetMapping("/info/{applyId}")
    public AjaxResult getInfo(@PathVariable Long applyId) {
        return AjaxResult.success(chatApplyService.getInfo(applyId));
    }

    /**
     * 同意申请
     */
    @ApiVersion(VersionEnum.V1_0_0)
    @PostMapping("/agree")
    public AjaxResult agree(@Validated @RequestBody ApplyVo01 applyVo) {
        chatFriendService.agree(applyVo.getApplyId());
        return AjaxResult.success();
    }

    /**
     * 拒绝申请
     */
    @ApiVersion(VersionEnum.V1_0_0)
    @PostMapping("/refused")
    public AjaxResult refused(@Validated @RequestBody ApplyVo01 applyVo) {
        chatFriendService.refused(applyVo.getApplyId());
        return AjaxResult.success();
    }

    /**
     * 忽略申请
     */
    @ApiVersion(VersionEnum.V1_0_0)
    @PostMapping("/ignore")
    public AjaxResult ignore(@Validated @RequestBody ApplyVo01 applyVo) {
        chatFriendService.ignore(applyVo.getApplyId());
        return AjaxResult.success();
    }

}
